<?php

namespace App\Http\Controllers;

use App\Comment;
use App\Post;
use App\Zan;
use Illuminate\Http\Request;

class PostController extends Controller
{
    //文章列表 get /posts index
    public function index(){
        $posts = Post::orderBy('created_at','desc')->withcount(['comments','zans'])->with('user')->paginate(6);
        return view('post.index',compact('posts'));
    }
    // 文章创建 get /posts/create  create
    public function create(){
        return view('post.create');
    }
    // 创建逻辑 post /posts store
    public function store(){
        // 表单验证
        $this->validate(request(),[
            'title' => 'required|String|max:100|min:5',
            'content' => 'required|String|min:10',
        ]);
        // 业务逻辑
        $user_id = \Auth::id();
        $params = array_merge(request(['title','content']),compact('user_id'));
        $post = Post::create($params);
        // 渲染
        return redirect("/posts");

    }
    // 文章详情 get /posts/{posts} show
    public function show(Post $post){
        $post->load('comments');
        //文章查看次数自增
        $post->increment('views');
        return view('post.show',compact('post'));
    }
    // 编缉文章 get /posts/{post}/edit
    public function edit(Post $post){
        return view('post.edit',compact('post'));
    }
    // 编缉逻辑 put /posts/{posts} update
    public function update(Post $post){
        // 验证
        $this->validate(request(),[
            'title'=>'required|String|max:100|min:5',
            'content' => 'required|String|min:10',
        ]);
        // 权限验证 当前用户是否能更新
        $this->authorize('update',$post);
        // 逻辑
        $post->title = request("title");
        $post->content = request('content');
        $post->save();
        // 渲染
        return redirect("/posts/{$post->id}");

    }
    // 删除文章 delete /posts/{posts} destroy
    public function delete(Post $post){
        // 权限认证
        $this->authorize('delete',$post);
        $post->delete();
        return redirect('/posts');
    }
    // 上传图片
    public function imageUpload(Request $request){
        $path = $request->file('wangEditorH5File')->storePublicly(date("YmdHis"));
        return asset('storage/'.$path);
    }
    // 提交评论
    public function comment(Post $post){
        // 验证
        $this->validate(request(),[
            'content'=>'required|min:3',
        ]);
        // 逻辑
        $comment = new Comment();
        $comment->user_id = \Auth::id();
        $comment->content = request('content');
        $post->comments()->save($comment);
        // 渲染
        return back();
    }
    // 点赞
    public function zan(Post $post){
        $params = [
            'user_id' => \Auth::id(),
            'post_id' => $post->id,
        ];
        Zan::firstOrCreate($params);
        return back();
    }
    // 取消赞
    public function unzan(Post $post){
        $post->zan(\Auth::id())->delete();
        return back();
    }
    // 文章搜索页
   /* public function search(){
        $this->validate(request(),[
            'query'=>'required',
        ]);
        $query = request('query');
        $posts = Post::search($query)->paginate(2);
        return view('post/search',compact('posts','query'));
    }*/
}
